

import UIKit

import AFNetworking

enum RequestMethod: String{
    case Post = "post"
    case Get = "get"
}

class NetworkingTool: AFHTTPSessionManager {
    
    static let shareWorkTools:NetworkingTool = {
       let tool = NetworkingTool()
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    typealias RequestCallBack = (reponseObject:AnyObject?, error: NSError?) -> ()
    
    //上传任务
    func upLoad(urlString:String,params:AnyObject,datas: [String: NSData],finished: RequestCallBack){
        
        self.POST(urlString, parameters: params, constructingBodyWithBlock: { (formdata) -> Void in
            //拼装要上传的数据
            
            
            for (key,value) in datas{
                formdata.appendPartWithFileData(value, name: key, fileName: "fs", mimeType: "application/octet-stream")
            }
            
            
            }, progress: nil, success: { (_, responseObject) -> Void in
                // 请求成功的回调
                finished(reponseObject: responseObject, error: nil)
            }) { (_, error) -> Void in
                finished(reponseObject: nil, error: error)
        }
        
        
    }
    
    
    
    
    
    func request(requestMethod:RequestMethod = .Get, urlString: String, parameters: AnyObject?, finished: RequestCallBack){
        
        let success = {(dataTask: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
            finished(reponseObject: responseObject, error: nil)
        }
        let failure = {(dataTask: NSURLSessionDataTask?, error: NSError?) -> () in
            finished(reponseObject: nil, error: error)
            
        }
        
        
        if requestMethod == .Get {
            GET(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        
        }else{
            POST(urlString, parameters: parameters, progress: nil , success: success, failure: failure)
        }
        
        
    }
    
    
}